package com.cande.loki.exercise.algorithm;

/**
 * @auther ROCK CODER
 * @DATE 2023/1/9 11:02
 * @Description 两个线程交替打印奇数偶数
 */
public class OddEven {
    private static volatile int i=1;

    public static void main(String[] args) {
        print(9);
//        new Thread(() -> {
//            while (i<=100){
//                if (i%2 ==0){
//                    System.out.println(i);
//                    i++;
//                }
//            }
//        }).start();
//        new Thread(() -> {
//            while (i<=100){
//                if (i%2 ==1){
//                    System.out.println(i);
//                    i++;
//                }
//            }
//        }).start();
    }
    public static volatile int x =1;
    public static void print(int i){
     new Thread(()->{
         while (x<=i){
             if (x%2 == 0){
                 System.out.println(x);
                 x++;
             }
         }
     }).start();

        new Thread(()->{
            while (x<=i){
                if (x%2 == 1){
                    System.out.println(x);
                    x++;
                }
            }
        }).start();
    }
}
